package org.sosie2.service.authentication;

import java.io.IOException;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Policy;
import java.security.ProtectionDomain;
import java.util.Enumeration;
import java.util.Properties;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.UnsupportedCallbackException;

import org.apache.ws.security.WSPasswordCallback;

/**
 * 
 * Is used by the WSSecurityEngine to check the validity of a username-password pair .
 * @author Gabriela
 */
public class SimpleCallbackHandler implements CallbackHandler {

//	Properties users = new Properties();
	
	public SimpleCallbackHandler() 
	{		
	}
	
	/**
	 * This function initializes the user-password properties map.
	 * @param p a properties instance
	 */
	public void init(Properties p)
	{
//		users = p;
	}

	/**
	 * This function is called to handle the callbacks.
	 */
	public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException 
	{
		for (int i = 0; i < callbacks.length; i++) 
		{
			if (callbacks[i] instanceof WSPasswordCallback) 
			{
				WSPasswordCallback pc = (WSPasswordCallback) callbacks[i];
				String username = pc.getIdentifer();
//				pc.setPassword(users.getProperty(username, ""));
				if (username.equals("gabi"))
					pc.setPassword("geheim");
			} 
			else 
			{
				throw new UnsupportedCallbackException(callbacks[i],"Unrecognized Callback");
			}
		}
	}
}
